#!/bin/bash

HAS_ERROR=0
function DO_CMD() {
    echo Exec command: $@
    $@
    EXIT_CODE=$?
    if [ $EXIT_CODE != 0 ]; then
        HAS_ERROR=1
        echo ERROR: Execute $@ failed.
        exit $EXIT_CODE
    else
        echo FINE: Execute success.
    fi
}

function CALL_ORA_CMD() {
    echo Exec command: $@
    $@
    exitCode=$?
    if [ $exitCode -ge 128 ]; then
        #负数返回码，大于127就是负数的补码
        echo "ERROR: Installation failed, exitCode:$exitCode."
    elif [ $exitCode -gt 0 ]; then
        echo "WARN: Installation has some warning, exitCode:$exitCode."
    else
        echo "FINE: All installations were successful."
    fi

    return $exitCode
}

function SUDO_ORA_CMD() {
    USER=$1
    shift
    echo "Exec command: su - '$USER' -c '$@'"
    eval "su - '$USER' -c '$@'"
    exitCode=$?
    if [ $exitCode -ge 128 ]; then
        #负数返回码，大于127就是负数的补码
        echo "ERROR: Execute command failed, exitCode:$exitCode."
        exit $exitCode
    elif [ $exitCode -gt 0 ]; then
        echo "WARN: Execute command  has some warning, exitCode:$exitCode."
    else
        echo "FINE: All operations were successful."
    fi
}

function removeUserAndGroups() {
    if [ -z "$ORACLE_UID" ]; then
        return
    fi

    usermod -g10 $ORACLE_USER

    #delete group id bigger than 1000
    groups=$(id -G $ORACLE_USER)
    for oraGid in $(id -G $ORACLE_USER); do
        if [ $oraGid -gt 1000 ]; then
            groupName=$(getent group GID | cut -d: -f1)
            groupdel $groupName
        fi
    done

    #delete oracle_user
    userdel $ORACLE_USER
}

#参数处理,全部用长参数
######################################
function parseOpts() {
    OPT_SPEC=":h-:"
    while getopts "$OPT_SPEC" optchar; do
        case "${optchar}" in
        -)
            case "${OPTARG}" in
            CV_ASSUME_DISTID)
                CV_ASSUME_DISTID="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            ORACLE_USER)
                ORACLE_USER="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            DEL_USER)
                DEL_USER="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            *)
                if [ "$OPTERR" = 1 ] && [ "${OPT_SPEC:0:1}" != ":" ]; then
                    echo "Unknown option --${OPTARG}" >&2
                fi
                ;;
            esac
            ;;
        h)
            usage
            exit 2
            ;;
        *)
            if [ "$OPTERR" != 1 ] || [ "${OPT_SPEC:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
            fi
            ;;
        esac
    done
}

parseOpts "$@"

if [ -z "$ORACLE_USER" ]; then
    ORACLE_USER=oracle
fi

ORACLE_UID=$(id -u $ORACLE_USER 2>/dev/null)
if [ -z "$ORACLE_UID" ]; then
    echo "WARN: User $ORACLE_USER not exists, maybe already uninstalled."
fi

ENV_DISTID=""
if [ "$CV_ASSUME_DISTID" != "AUTO" ]; then
    ENV_DISTID="CV_ASSUME_DISTID=$CV_ASSUME_DISTID"
fi

oraEnv=$(SUDO_ORA_CMD $ORACLE_USER env | grep ORACLE_)
echo "$oraEnv"
eval "$oraEnv"

oracleEnv=${oraEnv//$'\n'/\\n}
#Save oracle env
cat <<EOF >"output.json"
{
    "oracleEnv":"$oracleEnv"
}
EOF

if [ ! -f "$ORACLE_HOME/deinstall/deinstall" ]; then
    echo "WARN: Deinstall script $ORACLE_HOME/deinstall/deinstall not exists, maybe already uninstalled."
    exit 0
fi

SUDO_ORA_CMD $ORACLE_USER $ENV_DISTID LANG=en_US.UTF-8 $ORACLE_HOME/deinstall/deinstall -silent -checkonly -o /tmp | tee oradbunins_check.out
rspFile=$(grep "Location of response file generated:" oradbunins_check.out | cut -d: -f 2)
if [ -n "$rspFile" ]; then
    SUDO_ORA_CMD $ORACLE_USER $ENV_DISTID 'LANG=en_US.UTF-8 $ORACLE_HOME/deinstall/deinstall' -silent --paramfile $rspFile

    if [ "$DEL_USER" == "1" ]; then
        removeUserAndGroups
    fi
    echo "Uninstall success."
else
    echo "ERROR: Can not find respose file directory from deinstall prepare log."
    HAS_ERROR=1
fi

echo "INFO: Use dbclean to clean process and directory to do clean."

exit $HAS_ERROR
